package com.xuhuan.thread.study2.example;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author huan.xu
 * @date 2021/9/28 18:40
 * @description 多线程下载图片
 */
public class ThreadDownloadImage extends Thread{

    private String url;
    private String name;

    public ThreadDownloadImage(String url,String name){
        this.url=url;
        this.name=name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader=new WebDownloader();
        webDownloader.downloadFile(url,name);
        System.out.println("下载了文件名为：["+name+"]的文件");
    }

    public static void main(String[] args) {
        ThreadDownloadImage t1=new ThreadDownloadImage("http://n.sinaimg.cn/photo/700/w1000h500/20210603/57a6-kracxep9657657.png",
                "C:\\Users\\huan.xu\\Desktop\\1.png");
        ThreadDownloadImage t2=new ThreadDownloadImage("http://n.sinaimg.cn/photo/transform/700/w1000h500/20210524/5d99-kqpyffz1253659.jpg",
                "C:\\Users\\huan.xu\\Desktop\\2.jpg");
        ThreadDownloadImage t3=new ThreadDownloadImage("http://n.sinaimg.cn/news/transform/700/w1000h500/20210316/51c0-kmkptxe0299440.jpg",
                "C:\\Users\\huan.xu\\Desktop\\3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }

}

class WebDownloader{

    public void downloadFile(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
